<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Music extends Model
{
    protected $table = 'music';
     /**
      *  获取单条音乐
      */
     public static function getMusicById($id){
         return static :: find($id);
     }

    /**
     * @param $condition //查询条件
     * @param $fild // 查询字段
     * @param $order // 排序
     * @param $pagesize 分页
     */
     public static function getDataByCondition($condition,$filed,$order,$pagesize) {
            if($pagesize){
                 return static ::select($filed)->where($condition)->orderBy($order)->paginate($pagesize);
            }else{
                 return static ::select($filed)->where($condition)->orderBy($order)->get();
            }
     }

     /**
      * 获取首页数据
      */
     public static function getIndexData(){
           $dataCount = static ::count();
           $countRandId = rand(1,$dataCount-3);
           // 获取数据库中随机的一个值
           $data = static :: getMusicById($countRandId);
           $data = [0=>$data->toArray()];
           $id = $data[0]['id'];
           $datalist = static ::where('id', '>', $id)->take(3)->get();
           $datalist = $datalist->toArray();
           $newData = array_merge($data,$datalist);
           $listid = [];
           foreach ($datalist as $v){
                $listid[] = $v['id'];
           }
           return ['data'=>$newData,'listIds'=>json_encode($listid)];
     }

     /**
      * 获取下一个音乐数据
      */
     public function getNextInfo($id){
         $datalist = static ::where('id', '>', $id)->take(1)->get(['id','m_name','m_author']);
         return $datalist;
     }
}